<?php
//模块名_控制名
class Product_OffShelfController extends Zend_Controller_Action
{
	private $_ship_types;
	public function preDispatch(){
		
		set_time_limit(0);
		$this->tplDirectory = "product/offshelf/";
		$ship_types = Shipping_Service_ShipType::getAll();
		
		$ship_types__ = array();
		foreach($ship_types as $k=>$v){
			$ship_types__[$v['st_code']] = 0;
		}
// 		ksort($ship_types__);
		
		$this->_ship_types = $ship_types__;
			
	}
	
	public function indexAction(){		
		//产品品类
		$productSer = new Product_Service_Product();
		$categorys = $productSer->getAllCategory();
		$this->view->categorys = $categorys;
		$this->view->orderTypes = Order_Service_Order::getAllOrderType();
		
		$condition = array(	
			'orders_status'=>3,
			'process_status'=>1,
			//'import_status'=>array(1)
		);
		$orderby = array("count asc","shipping_method");
		$shipingMethod_count = Order_Service_Order::groupByShipMethod($condition,$orderby);
// 		print_r($shipingMethod_count);exit;
   		$ship_types__ = $this->_ship_types;

		foreach($shipingMethod_count as $v){
			unset($ship_types__[$v['shipping_method']]);
			$ship_types__[$v['shipping_method']] = $v['count'];
		}
		
		foreach($ship_types__ as $k=> $v){
			$ship_types__[$k] = $k.str_repeat("  ", 20-strlen($k.$v)).$v;
		}
		
		// 		$v['shipping_method'].str_repeat("-", 30-strlen($v['shipping_method'].$v['count'])).
		$this->view->ship_type = array_reverse($ship_types__,true);
		
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");
	}
	
	public function listAction(){
		$category = trim($this->_request->category);
		$orders_code = trim($this->_request->orders_code);
		$sku = trim($this->_request->sku);
		$orderType = trim($this->_request->orderType);
		$customerID = trim($this->_request->customer_id);
		$ship_type = trim($this->_request->ship_type);
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize',200);
		$import_status=$this->_request->getParam('import_status',1);
		$import_status = trim($import_status,",");
		$import_status = explode(",",$import_status);
		
		$category = empty($category)?0:$category;
		$pageSize = empty($pageSize)?100:$pageSize;
		$orders_code = empty($orders_code)?'':$orders_code;
		
		$sortColumn=$this->_request->getParam('sortColumn',"");
		$sortMethod=$this->_request->getParam('sortMethod',"asc");
		if(!empty($sortColumn)){
			$orderby = " $sortColumn $sortMethod";
		}else{
			$orderby = " add_time asc";			
		}
		$intpage = empty($intpage)?1:$intpage;
		
		$orders_code_list = array();
		if(!empty($sku)){
			$condition=array(
				'product_sku'=>$sku
			);
			$service_product = new Product_Service_Product();
			$products = $service_product->getByCondition($condition,'*');
			
			$productIds = array();
			foreach($products as $v){
				$productIds[] = $v['product_id'];
			}
			$contidion = array(
				"product_ids"=>$productIds
				);
			$order_products = Order_Service_Product::getByCondition($contidion,"*");
			foreach($order_products as $v){
				$orders_code_list[] = $v['orders_code'];
			}			
		}
		
		$dateType = $this->_request->getParam('dateType','add_time');//代码类型
		$dateFrom = $this->_request->getParam('dateFrom','');//代码类型
		$dateTo = $this->_request->getParam('dateTo','');//代码类型
		
		
// 		print_r($orders_code_list);exit;
		$condition = array(
			"orders_code"=>$orders_code,
			"orders_code_list"=>$orders_code_list,
			"category"=>$category,
			"orderType"=>$orderType,
			"customerID"=>$customerID,
			"shipping_method"=>$ship_type,
// 			'orders_status'=>3,
// 			'process_status'=>1,
			//'import_status'=>$import_status,				
			'dateType'=>$dateType,
			'dateFrom'=>$dateFrom,
			'dateTo'=>$dateTo,
		);
		
		
		
		
		
		$count = Product_Service_Offshelf::getPrintOrdersNew($condition,"count(*)");
		$orders = array();
		if($count>0){
			$orders = Product_Service_Offshelf::getPrintOrdersNew($condition,"*",$intpage,$pageSize,$orderby);			
		}
		$result = array("data"=>$orders,"count"=>$count,"status"=>Order_Service_Order::getAllProcessStatus());
		die(json_encode($result));
	}
	
	
	public function printOrdersAction(){
		$orderCodes = $this->_request->orderCode;
		$members = $this->_request->picking_members;
		
		if(empty($members)||empty($orderCodes)){			
			//die("未选择订单或未选择配货员");
		}
		
		
		$result = Product_Service_Offshelf::printOrders($orderCodes,$members);
		$this->view->result = $result;
		$this->view->err_orders = Zend_Json::encode($result['err_orders']);
		
		echo $this->view->render($this->tplDirectory."print-detail2.tpl");
	}
	
	public function printDetailAction(){
		$printResultSession = new Zend_Session_Namespace("printResult");
		$result = $printResultSession->result;
		$printResultSession->result = null;
		$this->view->result = $result;
		echo $this->view->render($this->tplDirectory."print-detail1.tpl");
	}
	
	
	public function hotFastAction(){
		//产品品类		
		$productSer = new Product_Service_Product();
		$categorys = $productSer->getAllCategory();
		$this->view->categorys = $categorys;
		$this->view->orderTypes = Order_Service_Order::getAllOrderType();
		
		$condition = array(
				'orders_status'=>3,
				'process_status'=>1,
				'import_status'=>array(1)
		);
		
		$orderby = array("count asc","shipping_method");
		$shipingMethod_count = Order_Service_Order::groupByShipMethod($condition,$orderby);
		
   		$ship_types__ = $this->_ship_types;

		foreach($shipingMethod_count as $v){
			unset($ship_types__[$v['shipping_method']]);
			$ship_types__[$v['shipping_method']] = $v['count'];
		}
		
		foreach($ship_types__ as $k=> $v){
			$ship_types__[$k] = $k.str_repeat("  ", 20-strlen($k.$v)).$v;
		}
		// 		$v['shipping_method'].str_repeat("-", 30-strlen($v['shipping_method'].$v['count'])).
		$this->view->ship_type = array_reverse($ship_types__,true);
		
		echo Cff::renderTpl($this->tplDirectory."hot-fast.tpl","layout");
	}
	
	public function createShipCodeAction(){
	
		$orderCodes = $this->_request->orderCode;
	
		if(empty($orderCodes)){
			$result = array("errors"=>array(array("msg"=>"未选择订单")),"orders_distinct"=>array());
			die(json_encode($result));
		}	
		$result = Order_Service_Order :: createShippingCode($orderCodes);		
			
		die(json_encode($result));
	}
	
	public function checkHotPrintOrdersAction(){
		
		$orderCodes = $this->_request->orderCode;
		$isIE = $this->_request->isIE;		
		$members = $this->_request->member;	
		
		if(empty($members)||empty($orderCodes)){
			//$result = array("errors"=>array(array("msg"=>"未选择订单或未选择配货员")),"orders_distinct"=>array());
			//die(json_encode($result));
		}
		
		$result = Product_Service_Offshelf::hotFastPrintOrdersNew($orderCodes,$members);
// 		print_r($result);exit;
		if(count($result['orders'])>0){
			$result['isIE'] = $isIE;	
			$printResultSession = new Zend_Session_Namespace("hotPrintResult");		
			$printResultSession->result = $result;	
		}			
			
		die(json_encode($result));			
	}
	
	public function hotPrintOrdersAction(){			
		$orderCodes = $this->_request->orderCode;
		$printResultSession = new Zend_Session_Namespace("hotPrintResult");
		$result = $printResultSession->result;
		if(empty($result)){
			echo "系统错误，没有订单提交";
			exit;
		}
		$printResultSession->result = null;			
		$this->view->result = $result;
		
		echo $this->view->render($this->tplDirectory."hot-print-detail_new1.tpl");
		
	}
	public function hotPrintDetailAction(){
		$printResultSession = new Zend_Session_Namespace("hotPrintResult");
		$result = $printResultSession->result;
		$printResultSession->result = null;
		$this->view->result = $result;
		echo $this->view->render($this->tplDirectory."hot-print.tpl");
	}
	
	public function getmemberAction(){
		$cond=array(
				'ud_id'=>12,
				'status'=>1
				);
		$member=User_Service_User::getByCond($cond, '*', 'user_id', 100, 0);
		if($member){
			$result = array(
					"data"=>$member,
					"ask"=>1);
		}else{
			$result = array(
					"ask"=>0);
		}
		
		die(json_encode($result));
	}

}